package com.xzy.c;

import com.xzy.service.BookService;
import com.xzy.service.CourseService;
import com.xzy.service.SchoolService;
import com.xzy.service.TeacherService;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/**
 * 依赖注入的方式：<bean autowire="byType"/> + set方法
 *
 * @author xzy
 * @date 2021/12/6  22:35
 */
@Slf4j
@ToString
public class ByTypeSetterServiceImpl {
    private BookService bookService;
    private CourseService courseService;
    private SchoolService schoolService;
    private TeacherService teacherService;

    public ByTypeSetterServiceImpl() {
        log.info("实例化 => ByTypeSetterServiceImpl()");
    }

    /*
     * byType + Setter：
     *     1、遍历Bean中的所有Setter
     *     2、尝试根据Setter参数的类型从容器中寻找依赖
     *     3、如果能够找到唯一的Bean，调用Setter完成依赖注入；如果能够找到多个Bean，抛出异常；如果找不到Bean，跳过该Set方法
     */

    public void setBookService(BookService bookService) {
        this.bookService = bookService;
        log.info("依赖注入 --> setBookService({})", bookService);
    }

    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
        log.info("依赖注入 --> setCourseService({})", courseService);
    }

    public void setSchoolService(SchoolService schoolService) {
        this.schoolService = schoolService;
        log.info("依赖注入 --> setSchoolService({})", schoolService);
    }

    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
        log.info("依赖注入 --> setTeacherService({})", teacherService);
    }

    /*
     * Spring会将满足下列条件的方法视为Setter；
     *     1.方法名setXxx()  2.有且只有一个参数
     *
     * 因此，是要能够在容器中找到唯一的TeacherService类的Bean，下面的方法就会被调用
     */

    public void setTeacherService123(TeacherService teacherService) {
        this.teacherService = teacherService;
        log.info("依赖注入 --> setTeacherService123({})", teacherService);
    }
}
